package com.zerdoor.service.impl;

import com.zerdoor.dao.PayDao;
import com.zerdoor.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by xiaoji on 2015/2/13.
 */
@Service
public class PayServiceImpl implements PayService {
    @Autowired
    private PayDao payDao;

    @Override
    public String getAlipayAccount() {
        Map<String,Object> map = payDao.getAlipayConfig(101001);
        if(map == null){
            return "";
        }else{
            return map.get("valueCn").toString();
        }
    }

    @Override
    public String getAlipayPartner() {
        Map<String,Object> map = payDao.getAlipayConfig(101002);
        if(map == null){
            return "";
        }else{
            return map.get("valueCn").toString();
        }
    }

    @Override
    public String getAlipayKey() {
        Map<String,Object> map = payDao.getAlipayConfig(101003);
        if(map == null){
            return "";
        }else{
            return map.get("valueCn").toString();
        }
    }

    @Override
    public void addUserWallet(Map<String, Object> map) throws Exception {
        payDao.addUserWallet(map);
    }

    @Override
    public void updateUserWalletSuccess(int walletId) throws Exception {
        //根据钱包充值编号获取钱包充值记录
        Map<String, Object> map = payDao.getUserWalletByWalletId(walletId);
        if(map != null){
            //钱包记录状态
            int walletStatus = Integer.parseInt(map.get("walletStatus").toString());
            //钱包充值状态必须是处理中的才可以做以下操作
            if(walletStatus == 131103 || walletStatus == 131102){
                //充值人编号id
                int userId = Integer.parseInt(map.get("userId").toString());
                //查这个人的钱包
                Map<String, Object> walletmap = payDao.getUserWallet(userId);
                BigDecimal wallet = new BigDecimal(walletmap.get("wallet").toString());
                BigDecimal curWallet = wallet.add(new BigDecimal(map.get("money").toString()));
                //冲入钱包金额
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("wallet",curWallet);
                params.put("userId",userId);
                params.put("walletId",walletId);
                payDao.updateWallet(params);
                //更改充值记录状态
                payDao.updateUserWalletSuccess(params);
            }

        }else{
            throw new Exception("支付成功查询充值结果失败！");
        }

    }

    @Override
    public void updateUserWalletFail(int walletId) throws Exception {
        //根据钱包充值编号获取钱包充值记录
        Map<String, Object> map = payDao.getUserWalletByWalletId(walletId);
        if(map != null){
            //钱包记录状态
            int walletStatus = Integer.parseInt(map.get("walletStatus").toString());
            if(walletStatus == 131103){
                //充值人编号id
                int userId = Integer.parseInt(map.get("userId").toString());
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("userId",userId);
                params.put("walletId",walletId);
                //更改充值记录状态
                payDao.updateUserWalletFail(params);
            }
        }else{
            throw new Exception("支付失败查询充值结果失败！");
        }

    }

}
